﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Training9
{
    public partial class frmStudentSel : Form
    {
        public frmStudentSel()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            SqlOperate obj = new SqlOperate();
            string strSql = "select * from Student where Sno like '%" + textBox1.Text + "%'";
            SqlDataReader odr = obj.GetDR(strSql);
            while (odr.Read())
            {

                DataGridViewRow row = new DataGridViewRow();
                int index = dataGridView1.Rows.Add(row);
                dataGridView1.Rows[index].Cells[0].Value = odr.GetValue(0).ToString();
                dataGridView1.Rows[index].Cells[1].Value = odr.GetValue(1).ToString();
                dataGridView1.Rows[index].Cells[2].Value = odr.GetValue(2).ToString();
                dataGridView1.Rows[index].Cells[3].Value = odr.GetValue(3).ToString();
                dataGridView1.Rows[index].Cells[4].Value = odr.GetValue(4).ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            SqlOperate obj = new SqlOperate();
            string strSql = "select * from Student";
            SqlDataReader odr = obj.GetDR(strSql);
            while (odr.Read())
            {

                DataGridViewRow row = new DataGridViewRow();
                int index = dataGridView1.Rows.Add(row);
                dataGridView1.Rows[index].Cells[0].Value = odr.GetValue(0).ToString();
                dataGridView1.Rows[index].Cells[1].Value = odr.GetValue(1).ToString();
                dataGridView1.Rows[index].Cells[2].Value = odr.GetValue(2).ToString();
                dataGridView1.Rows[index].Cells[3].Value = odr.GetValue(3).ToString();
                dataGridView1.Rows[index].Cells[4].Value = odr.GetValue(4).ToString();
            }
        }

        public int currindex;

        private void button3_Click(object sender, EventArgs e)
        {
            SqlOperate obj = new SqlOperate();
            string strSql = "UPDATE student SET SName ='" + textBox3.Text + "' WHERE sno='" + textBox2.Text + "'";
            if (obj.RunSql(strSql) > 0)
            {
                MessageBox.Show("修改记录成功！", "信息提示", MessageBoxButtons.OK);
                //dataGridView1.Rows[currindex].Cells[0].Value = textBox1.Text;
                //dataGridView1.Rows[currindex].Cells[1].Value = textBox2.Text;
            }
            else
            { MessageBox.Show("修改记录失败！", "信息提示", MessageBoxButtons.OK); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlOperate obj = new SqlOperate();
            string strSql = "DELETE FROM Student WHERE Sno='" + textBox4.Text + "'";
            if (obj.RunSql(strSql) > 0)
            {
                MessageBox.Show("删除记录成功！", "信息提示", MessageBoxButtons.OK);
                //dataGridView1.Rows.RemoveAt(currindex);
                //textBox2.Text = "";
                textBox4.Text = "";
            }
            else
            { MessageBox.Show("删除记录失败！", "信息提示", MessageBoxButtons.OK); }
        }
    }
}
